package gemini.castor.ui.client.page.content.account.myorder.form;

import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.page.widget.billingbox.BillingWidget;
import gemini.castor.ui.client.page.widget.dialog.GmnDialog;
import gemini.castor.ui.client.utils.HtmlUtils;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

public class MyOrderFormView extends Composite implements
		MyOrderFormPresenter.Display {

	@UiTemplate("MyOrderFormView.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, MyOrderFormView> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);
	private I18nConstants constants;
	private List<MyOrderFormModel> myOrderFormModels;

	public MyOrderFormView() {
		initWidget(binder.createAndBindUi(this));
		constants = GWT.create(I18nConstants.class);
	}

	@UiField
	FlexTable ftbMyOrder;

	@UiField
	HTML htmOrderInfo;

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void loadData() {

		initOrderTable();
		ftbMyOrder.removeAllRows();
		initOrderHeader();
		htmOrderInfo.setText(constants.htmOrderInfo());
		RowFormatter rowFmt = ftbMyOrder.getRowFormatter();

		int i = 1;
		for (final MyOrderFormModel model : myOrderFormModels) {
			if (i % 2 == 0) {
				rowFmt.setStyleName(i, "rowEven");
			}
			ftbMyOrder.setHTML(i, 0, i + "");
			ftbMyOrder.setHTML(i, 1, model.getCode());
			ftbMyOrder.setHTML(i, 2, model.getPurchaseName());
			ftbMyOrder.setHTML(i, 3, model.getOrderDate());
			ftbMyOrder.setHTML(i, 4, model.getOrderMonth());
			ftbMyOrder.setHTML(i, 5, model.getPoint());
			HTML htmStatus = new HTML(model.getStatus());
			htmStatus.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					BillingWidget billingWidget = new BillingWidget();
					billingWidget.loadData(model);
					billingWidget.setWidth("800px");
					GmnDialog dlgConfirm = new GmnDialog(constants.htmVAT(), billingWidget , "OK");
					dlgConfirm.show();

				}
			});

			ftbMyOrder.setWidget(i, 6, htmStatus);
		}
	}

	private void initOrderTable() {
		ColumnFormatter colFmt = ftbMyOrder.getColumnFormatter();

		colFmt.setWidth(0, "5%");
		colFmt.setWidth(1, "10%");
		colFmt.setWidth(2, "20%");
		colFmt.setWidth(3, "20%");
		colFmt.setWidth(4, "20%");
		colFmt.setWidth(5, "10%");
		colFmt.setWidth(6, "15%");
	}

	private void initOrderHeader() {
		ftbMyOrder.setHTML(0, 0, HtmlUtils.bold(constants.htmOrderNumber()));
		ftbMyOrder.setHTML(0, 1, HtmlUtils.bold(constants.htmCode()));
		ftbMyOrder.setHTML(0, 2, HtmlUtils.bold(constants.htmPurchaseName()));
		ftbMyOrder.setHTML(0, 3, HtmlUtils.bold(constants.htmOrderDay()));
		ftbMyOrder.setHTML(0, 4, HtmlUtils.bold(constants.htmOrderMonth()));
		ftbMyOrder.setHTML(0, 5, HtmlUtils.bold(constants.htmPoint()));
		ftbMyOrder.setHTML(0, 6, HtmlUtils.bold(constants.htmStatus()));
		ftbMyOrder.getRowFormatter().setStyleName(0, "tableHeader");
		ftbMyOrder.setStyleName("ftbSummary");
	}

	@Override
	public void setMyOrderFormModel(List<MyOrderFormModel> myOrderFormModels) {
		this.myOrderFormModels = myOrderFormModels;
	}

}
